﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;

public partial class 系统管理_add_gsdw : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        StringBuilder strSql = new StringBuilder();
        strSql.Append("insert into 归属单位表(");
        strSql.Append("id,name,info,lxr,phone)");
        strSql.Append(" values (");
        strSql.Append("@id,@name,@info,@lxr,@phone)");
        SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50),
					new SqlParameter("@name", SqlDbType.NVarChar,50),
					new SqlParameter("@info", SqlDbType.NVarChar,255),
					new SqlParameter("@lxr", SqlDbType.NVarChar,50),
					new SqlParameter("@phone", SqlDbType.NVarChar,50)};
        parameters[0].Value = this.id.Text;
        parameters[1].Value = this.name.Text;
        parameters[2].Value = this.info.Text;
        parameters[3].Value = this.lxr.Text;
        parameters[4].Value = this.phone.Text;

        SqlConnection conn = DataBase.getConnection();
       
        SqlTransaction tran = conn.BeginTransaction();
        try
        {
            SqlCommand cmd = new SqlCommand(strSql.ToString(), conn);
            if (parameters != null)
            {
                foreach (SqlParameter p in parameters)
                {
                    if (p != null)
                    {
                        cmd.Parameters.Add(p);
                    }
                }
            }
        }
        catch (SqlException sqlExp)
        {
            Console.Write(sqlExp.StackTrace);
            tran.Rollback();
            System.Web.HttpContext.Current.Server.ClearError(); // 清除当前的异常信息
        }
        catch (Exception exp)
        {
            Console.Write(exp.StackTrace);
            tran.Rollback();
            System.Web.HttpContext.Current.Server.ClearError(); // 清除当前的异常信息
        }

    }
}
